package app.binhexoct;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.Toast;
import android.util.Log;




public class binHexOct extends Activity {
	
	private ArrayAdapter<CharSequence> adapterForSpinner;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        final Button b = (Button) findViewById(R.id.get);
        b.setOnClickListener(CalcIt);
        
        Spinner s1 = (Spinner) findViewById(R.id.dtype);
        
        adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
        adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapterForSpinner);
        adapterForSpinner.add("Decimal");
        adapterForSpinner.add("Binary");
        adapterForSpinner.add("Octal");
        adapterForSpinner.add("Hexadecimal");
        
    }
    
    
    
    private OnClickListener CalcIt = new OnClickListener()
    {
        public void onClick(View v)
        {
        	final EditText t1 = (EditText) findViewById(R.id.num);
        	final TextView Result = (TextView) findViewById(R.id.result);
     
        	
        	Spinner s1 = (Spinner) findViewById(R.id.dtype);
   	
        	
        switch (s1.getSelectedItemPosition()){
    		case 0:
    			try{
        			ConvertFromDec(Integer.parseInt(t1.getText().toString()),Result);
        			break;
    			}
    			catch(Exception e)
    			{
    				Result.setText("Error: Check value is the correct format.");
    			}

    		case 1:
    			ConvertFromBin(t1.getText().toString(),Result);
    			break;
    		case 2:
    			ConvertFromOct(t1.getText().toString(),Result);
    			break;
    		case 3:
    			ConvertFromHex(t1.getText().toString(),Result);
    			break;
    	}

        }
    };
    
    public void ConvertFromDec(int value, TextView result)
    {
    	
    	try
    	{
    	
    		String hexString, octString, binString;
    	
    		hexString = Integer.toHexString(value).toUpperCase();
    		octString = Integer.toOctalString(value);
    		binString = Integer.toBinaryString(value);
		
    		result.setText("Hexidecimal Value: " + hexString + "\nOctal Value: " + octString + "\nBinary Value: " + binString);
    		result.setFreezesText(true);
        
    		Log.e("Decimal", "Dec");
        
		}
		catch(Exception e)
		{
			result.setText("Error: Check value is the correct format.");
		}
        
    	
    }
    
    public void ConvertFromHex(String value, TextView result)
    {
    	
    	try
    	{
    		
	    	String octString, binString;
	    	
	    	int decimal = Integer.parseInt(value,16);
			octString = Integer.toOctalString(decimal);
			binString = Integer.toBinaryString(decimal);
			
	        result.setText("Decimal Value: " + decimal + "\nOctal Value: " + octString + "\nBinary Value: " + binString);
	        result.setFreezesText(true);
	        
	        Log.e("Hexadecimal", "Hex");
        
		}
		catch(Exception e)
		{
			result.setText("Error: Check value is the correct format.");
		}
        
        
    	
    }
    
    public void ConvertFromOct(String value, TextView result)
    {
    	
    	try
    	{
    		
	    	String BinString, HexString;
	    	
	    	int decimal= Integer.parseInt(value,8);
			BinString = Integer.toBinaryString(decimal);
			HexString = Integer.toHexString(decimal);
			
	        result.setText("Decimal Value: " + decimal + "\nHexidecimal Value: " + HexString.toUpperCase() + "\nBinary Value: " + BinString);
	        result.setFreezesText(true);
	        
	        
	        Log.e("Octal", "Oct");
        
		}
		catch(Exception e)
		{
			result.setText("Error: Check value is the correct format.");
		}
        
    	
    }
    
    public void ConvertFromBin(String value, TextView result)
    {
    	
    	try
    	{
    	
	    	String OctString, HexString;
	    	
	    	int decimal= Integer.parseInt(value,2);
			OctString = Integer.toOctalString(decimal);
			HexString = Integer.toHexString(decimal);
			
	        result.setText("Decimal Value: " + decimal + "\nHexidecimal Value: " + HexString.toUpperCase() + "\nOctal Value: " + OctString);
	        result.setFreezesText(true);
	        
	        Log.e("Binary", "Bin");
        
		}
		catch(Exception e)
		{
			result.setText("Error: Check value is the correct format.");
		}
	    	
	    }
    

};



